//
//  RecomListViewController.m
//  WZCLApp
//
//  Created by chirui on 2018/7/3.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "RecomListViewController.h"
#import "MainTableViewCell.h"
#import "MainListDetailsViewController.h"

@interface RecomListViewController ()<HTHorizontalSelectionListDataSource,HTHorizontalSelectionListDelegate,UITableViewDelegate,UITableViewDataSource>{
    
    UITableView * _tableView;
    NSMutableArray * _titileArray;
    NSString * typeStr;
    
    NSMutableArray * _dataArray;
    int _page;
    int _totailPage;
    
    UIView * noBodyView;
}
@property(nonatomic,strong)HTHorizontalSelectionList * selectionList;

@end

@implementation RecomListViewController

-(void)viewWillAppear:(BOOL)animated{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [_tableView reloadData];
    });
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    TitleView(@"今日推荐");
    _titileArray =@[].mutableCopy;
    _dataArray = @[].mutableCopy;
    for (NSDictionary * dic in self.classArray) {
        
        [_titileArray addObject:dic[@"name"]];
    }
    
    typeStr  = self.classArray[_index][@"id"];
    
     self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_return_back" target:self action:@selector(backBtnClick)];
     noBodyView = [NothingMessageView creatNothingMessageViewFrame:CGRectMake(WIDTH/2-100, (HEIGHT-64)/2-100, 200, 200) andMessageStr:@"还没有相关内容~" onView:nil];
    
    [self creatTableView];
    [self getListDataRequest];
}

-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatTableView {
    
    _tableView  =[[UITableView alloc]initWithFrame:CGRectMake(0,0, WIDTH, HEIGHT-SH_NavBarHeight-SH_StatusBarHeight)
                                             style:UITableViewStyleGrouped];
    _tableView.dataSource=self;
    _tableView.delegate =self;
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView =view;
    
    [_tableView registerNib:[UINib nibWithNibName:@"MainTableViewCell" bundle:nil] forCellReuseIdentifier:[MainTableViewCell reuseIdentifier]];
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [_tableView reloadData];
        _page = 1;
        [self getListDataRequest];
        
    }];
    _tableView.mj_header = header;
    MJRefreshAutoNormalFooter *  footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page ++;
        [self getListDataRequest];
        
    }];
    footer.automaticallyRefresh = YES;
    _tableView.mj_footer =footer;
    
    if (_dataArray.count>10) {
        _tableView.mj_footer.hidden = NO;
    }else{
        _tableView.mj_footer.hidden = YES;
    }
    
    [self.view addSubview:_tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPat{
    
    return 100;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return  60;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = _dataArray[indexPath.row];
    MainTableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:[MainTableViewCell reuseIdentifier]];
    
    
    [cell.heardImage sd_setImageWithURL:[NSURL URLWithString:dic[@"cover"]] placeholderImage:[UIImage imageNamed:@"img_1"]];
    cell.titleLabel.frame = CGRectMake(109, 10, WIDTH - 120, 20);
    NSString * titleStr = dic[@"title"];
    cell.titleLabel.text = titleStr;
    
    cell.titleLabel.numberOfLines = 2;
    [cell.titleLabel sizeToFit];
    
    cell.detailsLabel.frame = CGRectMake(109, CGRectGetMaxY(cell.titleLabel.frame)+4, WIDTH - 140, 20);
    cell.numberLabel.text = dic[@"read"];
    cell.detailsLabel.text = dic[@"source"];
    cell.timeLabel.text = dic[@"create_time"];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = _dataArray[indexPath.row];
    MainListDetailsViewController * details = [[MainListDetailsViewController alloc]init];
    details.dataDic= dic;
    details.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:details animated:YES];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView * topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 60)];
    topView .backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.edgesForExtendedLayout =UIRectEdgeNone;
    
    UIView * scrollTitleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50)];
    scrollTitleView.backgroundColor = [UIColor whiteColor];
    
    _selectionList =[[HTHorizontalSelectionList alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50)];
    _selectionList.dataSource=self;
    _selectionList.delegate=self;
    [scrollTitleView addSubview:_selectionList];
    [_selectionList setTitleFont:[UIFont systemFontOfSize:15] forState:UIControlStateSelected];
    [_selectionList setTitleFont:[UIFont systemFontOfSize:15] forState:UIControlStateNormal];
    [_selectionList setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_selectionList setTitleColor:MyTabbarClick forState:UIControlStateSelected];
    //设置button之间的间距
    float WidthBtnFloat;
    if (ISP4||ISP5) {
        WidthBtnFloat =5;
    }else if (ISP6){
        WidthBtnFloat =10;
    }else {
        WidthBtnFloat =15;
    }
    _selectionList.buttonInsets= UIEdgeInsetsMake(5,WidthBtnFloat, 5, WidthBtnFloat);
    
    _selectionList.selectedButtonIndex = _index;
    //滑动按钮下边的滑块设置
    _selectionList.selectionIndicatorColor = MyTabbarClick;
    _selectionList.selectionIndicatorHeight =3;
    _selectionList.bottomTrimColor =[UIColor whiteColor];
    
    [topView addSubview:scrollTitleView];
    
    return topView;
}

#pragma mark------HTHorizontalSelectionListDelegate实现--
//点击效果
-(void)selectionList:(HTHorizontalSelectionList *)selectionList didSelectButtonWithIndex:(NSInteger)index{
    _index = index;
     typeStr  = self.classArray[_index][@"id"];
    UITableView *tableViwe=(id)[self.view viewWithTag:1000+index];
    [tableViwe reloadData];
    _page = 1;
    [self getListDataRequest];
    
    
}
//返回下表对应的文字按钮
-(NSString *)selectionList:(HTHorizontalSelectionList *)selectionList titleForItemWithIndex:(NSInteger)index{
    
    return _titileArray[index];
}
//多少个
-(NSInteger)numberOfItemsInSelectionList:(HTHorizontalSelectionList *)selectionList{
    
    return _titileArray.count;
    
}

-(void)getListDataRequest{
    
    if (_totailPage != 0) {
        if(_page == _totailPage + 1){
            [_tableView.mj_footer setState: MJRefreshStateNoMoreData];
            
            return ;
        }
    }
    NSDictionary * dic = @{@"page":[NSString stringWithFormat:@"%d",_page],
                           @"tag":@"news",
                           @"per_page":@"10",
                           @"type_id": typeStr,
                           @"if_hot":@"no"
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_mainNews_api) andparameters:dic Success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue]==200) {
            
            _totailPage = [responseObject[@"datas"][@"total_page"]  intValue];
            
            if (_page==1) {
                
                if (_dataArray) {
                    
                    [_dataArray removeAllObjects];
                }
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
                
            }else {
                
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }
            if (_dataArray.count>0) {
                noBodyView.hidden = YES;
                if (_dataArray.count<10) {
                    
                    _tableView.mj_footer.hidden = YES;
                }
            }else {
                noBodyView.hidden = NO;
                [self.view addSubview:noBodyView];
                _tableView.mj_footer.hidden = YES;
                
            }
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
            
        }
        
        [_tableView.mj_footer endRefreshing];
        [_tableView.mj_header  endRefreshing];
        [_tableView reloadData];
        
        
    } failed:^(NSError *error) {
        [_tableView.mj_header endRefreshing];
        [_tableView reloadData];
        SHuiMBhudTextTimeStr(@"网络错误，请稍后再试");
    }];
    
    
}

@end
